import { mobileReg } from '@/utils/tools/validate'
import store from '@/store'
import CONSTANT from '@/constant/index'
const isEmpty = (val) => {
	if (val instanceof Array) {
		if (val.length === 0) return true
	} else if (val instanceof Object) {
		if (!Object.keys(val).length) return true
	} else {
		if (val === 'null' || val === null || val === 'undefined' || val === undefined || val === '') return true
		return false
	}
	return false
}

// 拼接地址栏
const spliceUrl = (url, query) => {
	let newUrl = ''
	url = url.indexOf('/') == 0 ? url : `/${url}`
	if (url.indexOf('?') > -1) {
		newUrl = `${url}&${query}`
	} else {
		newUrl = `${url}?${query}`
	}
	return newUrl
}

// 屏蔽微信审核的相关模块
const shieldReview = () => {
	let config = uni.getStorageSync('allConfig')
	config = config ? JSON.parse(config).config : {}
	// const basicUserInfo = store.state.user.basicUser
	// const adminUserInfo = store.state.user.user
	// const userInfo = adminUserInfo || basicUserInfo
	return config.hkds_wxapp_version != CONSTANT.version
	// return userInfo.user_id != 18048 && userInfo.hkds_get_mobile != 0
}

/** *
 * 对象参数转为url参数
 * @parmas query 拼接得参数对象
 */
export const parse = (query) => {
	return Object.keys(query)
	  .filter(key => !isEmpty(query[key]))
	  .reduce((result, key) => {
			const value = query[key]
			// in查询特殊处理
			if (Array.isArray(value) && !isEmpty(value)) {
		  return `${result}&${value.reduce((val, cVal) => `${val ? `${val}&` : val}${key}=${cVal}`, '')}`
			}

			// between查询做特殊处理
			if (typeof value === 'object' && !isEmpty(value)) {
		  const [start, end] = value
		  return `${result}&${key}[]=${start}&${key}[]=${end}`
			}

			return `${result}&${key}=${value}`
	  }, '')
	  .replace(/^&/, '?')
}

const $checkMobile = (num) => {
	return mobileReg.test(num)
}
const $checkName = (str) => {
	return /^[\u4E00-\u9FA5A-Za-z\s]+(·[\u4E00-\u9FA5A-Za-z]+)*$/.test(str)
}

export const $formatDate = (num, fmt = 'YYYY-MM-DD HH:mm:ss') => {
	let date = new Date()
	date.setTime(num)
	let o = {
		'M+': date.getMonth() + 1,
		'D+': date.getDate(),
		'h+': date.getHours() % 12 === 0 ? 12 : date.getHours() % 12,
		'H+': date.getHours(),
		'm+': date.getMinutes(),
		's+': date.getSeconds(),
		'q+': Math.floor((date.getMonth() + 3) / 3),
		'S': date.getMilliseconds()
	}
	let week = {
		'0': '\u65e5',
		'1': '\u4e00',
		'2': '\u4e8c',
		'3': '\u4e09',
		'4': '\u56db',
		'5': '\u4e94',
		'6': '\u516d'
	}
	if (/(Y+)/.test(fmt)) {
		fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
	}
	if (/(E+)/.test(fmt)) {
		fmt = fmt.replace(RegExp.$1, ((RegExp.$1.length > 1) ? (RegExp.$1.length > 2 ? '\u661f\u671f' : '\u5468') : '') + week[date.getDay() + ''])
	}
	for (let k in o) {
		if (new RegExp('(' + k + ')').test(fmt)) {
			fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)))
		}
	}
	return fmt
}
export default {
	isEmpty,
	parse,
	$checkMobile,
	$checkName,
	$formatDate,
	spliceUrl,
	shieldReview
}
